package com.example.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.example.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import com.example.utils.Result;
import com.example.vod.service.VodService;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/vod/video/")
public class VodController {
	
	@Autowired
	VodService vodService;
	
	@PostMapping("upload")
	public Result uploadVideo(MultipartFile file) {
		
		String videoId = vodService.uploadVideo(file);
		return Result.ok().data("videoId",videoId);
	}
	
	@DeleteMapping("deleteVideo/{id}")
	public Result deleteVideo(@PathVariable String id) {
		vodService.deleteVideo(id);
		
		return Result.ok();
	}

	@DeleteMapping("deleteVideos")
	public Result deleteVideos( @RequestParam("videoIdList") List<String> ids) {
		vodService.deleteVideos(ids);

		return Result.ok();
	}
///admin/vod/video/getPlayAuth/${videoId}
	@GetMapping("getPlayAuth/{videoId}")
	public Result getVideoPlayAuth(@PathVariable("videoId") String videoId) throws Exception {
		System.out.println(videoId);

		//获取阿里云存储相关常量
		String accessKeyId = "LTAI4GJPGzBeJ5jNNniq6kAj";
		String accessKeySecret = "P01LGdhFP3ghj7VZ8H1dZnTRPrPoRh";
		System.out.println("获取凭证");

		//初始化
		DefaultAcsClient client = InitVodClient.initVodClient(accessKeyId, accessKeySecret);

		//请求
		GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
		request.setVideoId(videoId);

		//响应
		GetVideoPlayAuthResponse response = null;
		try {
			response = client.getAcsResponse(request);
		} catch (ServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String playAuth = response.getPlayAuth();
		
		System.out.println(playAuth);

		//返回结果
		return Result.ok().message("获取凭证成功").data("playAuth", playAuth);
	}

}
